/---------------------------
// Includes
//---------------------------
#include "StdAfx.h"
#include "EmptyApp.h"
#include "ContentManager.h"
#include "InputStateManager.h"
#include "RenderContext.h"
#include "Level.h"
#include "GraphCamera.h"
#include "Camera.h"
#include "BaseCamera.h"
#include "Character.h"
#include "DX10ObjMesh.h"
#include "ObjMesh.h"
#include "IniFile.h"
#include "ObjReader.h"
#include "PhysicsMesh.h"
#include "LightReader.h"
#include "SpherePNT.h"
#include "SimpleBone.h"
#include "SoftwareSkinMesh.h"
#include "BinAnimationReader.h"
#include "FullScreenQuad.h"
#include "DrawableTex2D.h"
#include <cmath>
#include "SpikeySphere.h"
#include "ShadowCamera.h"
#include "PhysX.h"
#include "Math.h"
#include "MiniMap.h"
#include "Interface.h"
#include "InterfaceItems.h"
#include "PhysicsDebugRenderer.h"
#include "Terrain.h"
#include "SkyBox.h"
#include "QuadPNT.h"
#include "SearchCell.h"
#include "PathFinding.h"
#include "Building.h"
#include "Soldier.h"
#include "AI.h"

#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "libexpatw.lib")
#pragma comment(lib, "objReader.lib")
//---------------------------
// Defines
//---------------------------
//#define GAME_ENGINE (GameEngine::GetSingleton())

#define TIMEFORNEXTMOVE 3

//---------------------------
// Constructor & Destructor
//---------------------------
AI::AI(ContentManager *ContentManager, Level* level) : m_pContentManager(ContentManager),m_pLevel(level), m_Money(6000), m_IsHarvester(false), m_IsMainStructure(true),
	m_IsBarracks(false), m_Time(0), m_MaxNumberSoldiers(0), m_OnlyOnce(false), m_IsUnderAttack(false)
{
	// nothing to create
}

AI::~AI()
{

}

void AI::Initialize(ObjMesh* tower, ObjMesh* harvester, ObjMesh* house, ObjMesh* main, ObjMesh* barracks)
{
	m_TowerMesh = tower;
	m_HarvesterMesh = harvester;
	m_HouseMesh = house;
	m_MainMesh = main;
	m_BarracksMesh = barracks;

	LightReader lr (_T("./Resources/Models/meshes/lights.xml"));
	lr.Read();

	Building *build = new Building(m_MainMesh, 10, 15, 10, 1000, true, m_pContentManager);
	build->AddSpotLights(lr.GetLights());
	m_pLevel->AddChild(build);	
	
	//build->Initialize(m_pContentManager);
	build->SetNumber(2);
	m_Buildinglist.push_back(build);

}

void AI::Tick(const InputState & refInputState)
{	
	if(m_OnlyOnce == false)
	{	
		m_Buildinglist.at(0)->Scale(D3DXVECTOR3(0.2f,0.2f,0.2f));
		m_Buildinglist.at(0)->Translate(D3DXVECTOR3(D3DXVECTOR3(80, 0, 80)));

		m_Buildinglist.at(0)->SetTriggerPos(D3DXVECTOR3(80, 0, 80));
		m_OnlyOnce = true;
	}
	MoveHarvester();
	CheckDefence(refInputState);

	for(int i = 0; m_Soldierlist.size() > i; ++i)
	{
		m_Soldierlist.at(i)->Move();
	}

	for(int i = 0; m_SoldierAttackinglist.size() > i; ++i)
	{
		for(int j = 0; m_SoldierAttackinglist.at(i).size() > j; ++j)
		{
			m_SoldierAttackinglist.at(i).at(j)->Move();
		}
	}

	m_Time += refInputState.GetDeltaTime();

	UpdateAttack(refInputState);

	int NumberAttackers = 0;
	for(int i = 0;  m_SoldierAttackinglist.size() > i; ++i)
	{
		for(int j = 0;  m_SoldierAttackinglist.size() > j; ++j)
		{
			NumberAttackers++;
		}
	}

	if(m_Time >= TIMEFORNEXTMOVE)
	{
		if(m_IsHarvester == false)
		{
			if(m_Money >= 2000)
			{
				Building * build = new Building(m_HarvesterMesh, 4, 9, 6.5, 400, false, m_pContentManager);
				build->SetNumber(2);	
			
				

				build->Scale(D3DXVECTOR3(0.2f,0.2f,0.2f));

				int RandomX = (rand()%30)+75;
				int RandomZ = (rand()%30)+75;

				build->Translate(D3DXVECTOR3(RandomX, 0, RandomZ));
				m_pLevel->AddChild(build);

				LightReader lr (_T("./Resources/Models/meshes/lights.xml"));
				lr.Read();

				build->Initialize(m_pContentManager);
				build->AddSpotLights(lr.GetLights());
				m_Buildinglist.push_back(build);

				m_CostPrice = 2000;

				bool FindPos = false;

				while(FindPos == false)
				{
					FindPos = SeeIfFree();

					if(FindPos == false)
					{
						D3DXVECTOR4 pos;
						D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &build->GetWorldMatrix());

						RandomX = (rand()%30)+75;
						RandomZ = (rand()%30)+75;

						float DistanceX = RandomX - pos.x;
						float DistanceZ = RandomZ - pos.z;

						build->Translate(D3DXVECTOR3(DistanceX, 0, DistanceZ));
					}
				}

				m_Money -= m_CostPrice;

				D3DXVECTOR4 pos;
				D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &build->GetWorldMatrix());

				build->SetTriggerPos(D3DXVECTOR3(pos.x, pos.y, pos.z));

				SpikeySphere* m_TestSphere = new SpikeySphere(_T("./Resources/Textures/GroteKogel.jpg"));
				m_TestSphere->Translate(D3DXVECTOR3(pos.x, 3, pos.z - 8));
				m_TestSphere->m_PosHome = D3DXVECTOR3(pos.x, 3, pos.z - 8);
				m_TestSphere->SetOpacity(1);
				m_pLevel->AddChild(m_TestSphere);
				m_TestSphere->Initialize(m_pContentManager);
				m_Harvesterlist.push_back(m_TestSphere);

				//new harvester pathfinding
				PathFinding* path = new PathFinding(m_pLevel);
				m_HarvesterPathList.push_back(path);

				m_IsHarvester = true;
			}
		}

		else if(m_IsBarracks == false)
		{
			if(m_Money >= 2000)
			{
				m_Barracks = new Building(m_BarracksMesh, 7, 6, 10, 400, false, m_pContentManager);
				m_Barracks->SetNumber(2);	
			
				int RandomX = (rand()%60)+50;
				int RandomZ = (rand()%60)+50;

				m_Barracks->Scale(D3DXVECTOR3(0.2f,0.2f,0.2f));
				m_Barracks->Translate(D3DXVECTOR3(RandomX, 0, RandomZ));
				m_pLevel->AddChild(m_Barracks);

				LightReader lr (_T("./Resources/Models/meshes/lights.xml"));
				lr.Read();

				m_Barracks->Initialize(m_pContentManager);
				m_Barracks->AddSpotLights(lr.GetLights());
				m_Buildinglist.push_back(m_Barracks);

				m_CostPrice = 2000;

				bool FindPos = false;

				while(FindPos == false)
				{
					FindPos = SeeIfFree();

					if(FindPos == false)
					{
						D3DXVECTOR4 pos;
						D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &m_Barracks->GetWorldMatrix());

						RandomX = (rand()%60)+50;
						RandomZ = (rand()%60)+50;

						float DistanceX = RandomX - pos.x;
						float DistanceZ = RandomZ - pos.z;

						m_Barracks->Translate(D3DXVECTOR3(DistanceX, 0, DistanceZ));
					}
				}

				m_Money -= m_CostPrice;

				D3DXVECTOR4 pos;
				D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &m_Barracks->GetWorldMatrix());

				m_Barracks->SetTriggerPos(D3DXVECTOR3(pos.x, pos.y, pos.z));

				m_IsBarracks= true;
			}
		}

		else if((m_Soldierlist.size() + NumberAttackers) < m_MaxNumberSoldiers)
		{
			if(m_Soldierlist.size() != 5)
			{
				if(m_Money >= 200)
				{
					Soldier* SmallSoldier = new Soldier(_T("./Resources/Textures/GroteKogel.jpg"), 1, 15, 15, m_pContentManager, 50, 20, 2);					

					D3DXVECTOR4 pos;
					D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &m_Barracks->GetWorldMatrix());
					SmallSoldier->SetNumber(1);
					
					m_pLevel->AddChild(SmallSoldier);
					SmallSoldier->Initialize(m_pContentManager);
					SmallSoldier->Translate(D3DXVECTOR3(pos.x + 10, 3, pos.z));
					SmallSoldier->FindPosition();
					m_Soldierlist.push_back(SmallSoldier);

					PathFinding* Path = new PathFinding(m_pLevel);

					m_DefencePathList.push_back(Path);

					m_Money -= 200;

					if(m_Soldierlist.size() >= 6)
					{
						vector<Soldier*> newlist;
						newlist.push_back(m_Soldierlist.at(3));
						newlist.push_back(m_Soldierlist.at(4));
						newlist.push_back(m_Soldierlist.at(5));

						m_SoldierAttackinglist.push_back(newlist);

						m_Soldierlist.pop_back();
						m_Soldierlist.pop_back();
						m_Soldierlist.pop_back();

						StartAttackSoldiers();
					}
				}
			}

			else
			{
				if(m_Money >= 500)
				{
					Soldier* SmallSoldier = new Soldier(_T("./Resources/Textures/GroteKogel.jpg"), 3, 15, 15, m_pContentManager, 100, 35, 2);					

					D3DXVECTOR4 pos;
					D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &m_Barracks->GetWorldMatrix());
					SmallSoldier->SetNumber(1);
					
					m_pLevel->AddChild(SmallSoldier);
					SmallSoldier->Initialize(m_pContentManager);
					SmallSoldier->Translate(D3DXVECTOR3(pos.x + 10, 3, pos.z));
					SmallSoldier->FindPosition();
					m_Soldierlist.push_back(SmallSoldier);

					PathFinding* Path = new PathFinding(m_pLevel);

					m_DefencePathList.push_back(Path);

					m_Money -= 200;

					if(m_Soldierlist.size() >= 6)
					{
						vector<Soldier*> newlist;
						newlist.push_back(m_Soldierlist.at(3));
						newlist.push_back(m_Soldierlist.at(4));
						newlist.push_back(m_Soldierlist.at(5));

						m_SoldierAttackinglist.push_back(newlist);

						m_Soldierlist.pop_back();
						m_Soldierlist.pop_back();
						m_Soldierlist.pop_back();

						StartAttackSoldiers();
					}
				}
			}
		}

		else if(m_Money >= 500)
		{
			Building * build = new Building(m_HouseMesh, 4, 3, 4, 200, false, m_pContentManager);
			build->SetNumber(2);	
			
			int RandomX = (rand()%60)+50;
			int RandomZ = (rand()%60)+50;

			build->Scale(D3DXVECTOR3(0.1f,0.1f,0.1f));
			build->Translate(D3DXVECTOR3(RandomX, 0, RandomZ));
			m_pLevel->AddChild(build);

			LightReader lr (_T("./Resources/Models/meshes/lights.xml"));
			lr.Read();

			build->Initialize(m_pContentManager);
			build->AddSpotLights(lr.GetLights());
			m_Buildinglist.push_back(build);

			m_CostPrice = 500;

			bool FindPos = false;

			while(FindPos == false)
			{
				FindPos = SeeIfFree();

				if(FindPos == false)
				{
					D3DXVECTOR4 pos;
					D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &build->GetWorldMatrix());

					RandomX = (rand()%60)+50;
					RandomZ = (rand()%60)+50;

					float DistanceX = RandomX - pos.x;
					float DistanceZ = RandomZ - pos.z;

					build->Translate(D3DXVECTOR3(DistanceX, 0, DistanceZ));
				}
			}

			m_Money -= m_CostPrice;

			D3DXVECTOR4 pos;
			D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &build->GetWorldMatrix());

			build->SetTriggerPos(D3DXVECTOR3(pos.x, pos.y, pos.z));
			m_MaxNumberSoldiers += 3;
		}
		m_Time = 0;
	}
}

bool AI::SeeIfFree()
{
	bool IsNotPosible = false;

	int x = m_Buildinglist.back()->GetX();
	int y = m_Buildinglist.back()->GetY();
	int z = m_Buildinglist.back()->GetZ();

	D3DXVECTOR4 pos;
	D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &m_Buildinglist.back()->GetWorldMatrix());

	NxRay Raycast;

	Raycast.orig = NxVec3(pos.x - x - 0.1f, 20, pos.z - z -0.1f);
	Raycast.dir = NxVec3(0, -1, 0);
	Raycast.dir.normalize();

	NxRaycastHit GetIntercect;

	NxShape* ClosestShape = m_pLevel->GetPhysicsScene()->raycastClosestShape(Raycast, NX_ALL_SHAPES, GetIntercect);

	if(ClosestShape)
	{
		if(GetIntercect.worldImpact.y > 0.2f) IsNotPosible = true;
	}


	Raycast.orig = NxVec3(pos.x - x - 0.1f, 20, pos.z + z + 0.1f);
	Raycast.dir = NxVec3(0, -1, 0);
	Raycast.dir.normalize();

	GetIntercect;

	ClosestShape = m_pLevel->GetPhysicsScene()->raycastClosestShape(Raycast, NX_ALL_SHAPES, GetIntercect);

	if(ClosestShape)
	{
		if(GetIntercect.worldImpact.y > 0.2f) IsNotPosible = true;
	}

	Raycast.orig = NxVec3(pos.x, 20, pos.z);
	Raycast.dir = NxVec3(0, -1, 0);
	Raycast.dir.normalize();

	GetIntercect;

	ClosestShape = m_pLevel->GetPhysicsScene()->raycastClosestShape(Raycast, NX_ALL_SHAPES, GetIntercect);

	if(ClosestShape)
	{
		if(GetIntercect.worldImpact.y > 0.2f) IsNotPosible = true;
	}


	Raycast.orig = NxVec3(pos.x + x + 0.1f, 20, pos.z + z + 0.1f);
	Raycast.dir = NxVec3(0, -1, 0);
	Raycast.dir.normalize();

	GetIntercect;

	ClosestShape = m_pLevel->GetPhysicsScene()->raycastClosestShape(Raycast, NX_ALL_SHAPES, GetIntercect);

	if(ClosestShape)
	{
		if(GetIntercect.worldImpact.y > 0.2f) IsNotPosible = true;
	}


	Raycast.orig = NxVec3(pos.x + x + 0.1f, 20, pos.z - z - 0.1f);
	Raycast.dir = NxVec3(0, -1, 0);
	Raycast.dir.normalize();

	GetIntercect;

	ClosestShape = m_pLevel->GetPhysicsScene()->raycastClosestShape(Raycast, NX_ALL_SHAPES, GetIntercect);

	if(ClosestShape)
	{
		if(GetIntercect.worldImpact.y > 0.2f) IsNotPosible = true;
	}

	if(IsNotPosible == false) return true;
	else return false;
}

void AI::MoveHarvester()
{
	for(int i = 0; i < m_Harvesterlist.size(); ++i)
		{
			if(m_Harvesterlist.at(i)->m_GotPath == false)
			{
				if(m_HarvesterPathList.at(i)->m_foundGoal == false)
				{				
					D3DXVECTOR4 pos;
					D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &m_Harvesterlist.at(i)->GetWorldMatrix());

					if(m_Harvesterlist.at(i)->m_Home == true)	m_HarvesterPathList.at(i)->FindPath(D3DXVECTOR3(pos.x, 3, pos.z), D3DXVECTOR3(100, 3, 95));
					else	m_HarvesterPathList.at(i)->FindPath(D3DXVECTOR3(100, 3, 95), D3DXVECTOR3(m_Harvesterlist.at(i)->m_PosHome.x, 3, m_Harvesterlist.at(i)->m_PosHome.z));
				}

				if(m_HarvesterPathList.at(i)->m_foundGoal == true)
				{
					m_Harvesterlist.at(i)->SetPath(m_HarvesterPathList.at(i)->GetPath());
					m_Harvesterlist.at(i)->m_GotPath = true;
				}
			}

			else
			{
				m_Harvesterlist.at(i)->Move();
			}	

			if(m_Harvesterlist.at(i)->m_GotDestination == true)
			{
				m_Harvesterlist.at(i)->SetHome();
				if(m_Harvesterlist.at(i)->m_Home == true) m_Money += 25;
				m_Harvesterlist.at(i)->m_GotPath = false;
				m_Harvesterlist.at(i)->m_GotDestination = false;
				m_HarvesterPathList.at(i) = new PathFinding(m_pLevel);
			}
		}
}

void AI::CheckForRemove()
{
	for(int i = 0; m_Soldierlist.size() > i ; ++i)
	{
		if(m_Soldierlist.at(i)->GetDeleted())
		{
				m_Soldierlist.at(i)->RemoveSphere();

				m_Soldierlist.at(i)->SetParent(NULL);

				vector<Soldier*>::iterator foundElement;
				foundElement = std::find(m_Soldierlist.begin(),m_Soldierlist.end(),m_Soldierlist.at(i));
				if ( foundElement != m_Soldierlist.end())
				{
						m_Soldierlist.erase(foundElement);
				}

				m_DefencePathList.pop_back();
		}
	}

	for(int i = 0; m_SoldierAttackinglist.size() > i ; ++i)
	{
		for(int j = 0; m_SoldierAttackinglist.at(i).size() > j ; ++j)
		{
			if(m_SoldierAttackinglist.at(i).at(j)->GetDeleted())
			{
					m_SoldierAttackinglist.at(i).at(j)->RemoveSphere();

					m_SoldierAttackinglist.at(i).at(j)->SetParent(NULL);

					vector<Soldier*>::iterator foundElement;
					foundElement = std::find(m_SoldierAttackinglist.at(i).begin(),m_SoldierAttackinglist.at(i).end(),m_SoldierAttackinglist.at(i).at(j));
					if ( foundElement != m_SoldierAttackinglist.at(i).end())
					{
							m_SoldierAttackinglist.at(i).erase(foundElement);
					}			
			}
		}
	}

	if(m_SoldierAttackinglist.size() != 0)
	{
		if(m_SoldierAttackinglist.at(0).size() == 0)
		{
			vector<PathFinding*>::iterator foundElement;
			foundElement = std::find(m_MakePathList.begin(),m_MakePathList.end(),m_MakePathList.at(0));
			if ( foundElement != m_MakePathList.end())
			{
					m_MakePathList.erase(foundElement);
			}

			vector<vector<Soldier*>>::iterator foundElement2;
			foundElement2 = std::find(m_SoldierAttackinglist.begin(),m_SoldierAttackinglist.end(),m_SoldierAttackinglist.at(0));
			if ( foundElement2 != m_SoldierAttackinglist.end())
			{
					m_SoldierAttackinglist.erase(foundElement2);
			}
		}
	}

	for(int i = 0; m_Buildinglist.size() > i ; ++i)
	{
		if(m_Buildinglist.at(i)->GetDeleted())
		{
				if(m_Buildinglist.at(i)->GetMesh() == m_BarracksMesh) m_IsBarracks = false;
				if(m_Buildinglist.at(i)->GetMesh() == m_HarvesterMesh)
				{
					D3DXVECTOR4 pos;
					D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &m_Buildinglist.at(i)->GetWorldMatrix());

					for(int i = 0; m_Harvesterlist.size() > i ; ++i)
					{
						D3DXVECTOR3 home = m_Harvesterlist.at(i)->m_PosHome;
						if(pos.x == home.x && (pos.z- 8) == home.z )
						{
							m_pLevel->RemoveChild(m_Harvesterlist.at(i));

							vector<SpikeySphere*>::iterator foundElement;
							foundElement = std::find(m_Harvesterlist.begin(),m_Harvesterlist.end(),m_Harvesterlist.at(i));
							if ( foundElement != m_Harvesterlist.end())
							{
									m_Harvesterlist.erase(foundElement);
							}

							vector<PathFinding*>::iterator foundElement2;
							foundElement2 = std::find(m_HarvesterPathList.begin(),m_HarvesterPathList.end(),m_HarvesterPathList.at(i));
							if ( foundElement2 != m_HarvesterPathList.end())
							{
									m_HarvesterPathList.erase(foundElement2);
							}
						}
					}
					
					m_IsHarvester = false;
					
				}
				if(m_Buildinglist.at(i)->GetMesh() == m_MainMesh) m_IsMainStructure = false;
				if(m_Buildinglist.at(i)->GetMesh() == m_HouseMesh) m_MaxNumberSoldiers -= 3; 

				m_Buildinglist.at(i)->RemoveThis();

				m_Buildinglist.at(i)->SetParent(NULL);

				vector<Building*>::iterator foundElement;
				foundElement = std::find(m_Buildinglist.begin(),m_Buildinglist.end(),m_Buildinglist.at(i));
				if ( foundElement != m_Buildinglist.end())
				{
						m_Buildinglist.erase(foundElement);
				}
		}
	}
}

void AI::StartAttackSoldiers()
{	
	float MidX = 0;
	float MidZ = 0;
	float NumbersOf = 0;

	for(int i = 0; m_SoldierAttackinglist.back().size() > i; ++i)
	{
		D3DXVECTOR4 pos;
		D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &m_SoldierAttackinglist.back().at(i)->GetWorldMatrix());

		MidX += pos.x;
		MidZ += pos.z;

		++NumbersOf;	
	}

	MidX /= NumbersOf;
	MidZ /= NumbersOf;

	PathFinding* path = new PathFinding(m_pLevel);

	m_MakePathList.push_back(path);

	if(NumbersOf !=0)
	{
		int RandomX = (rand()%3)+1;
		if(RandomX == 1) m_MakePathList.back()->FindPath(D3DXVECTOR3(MidX, 3, MidZ), D3DXVECTOR3(-100,3,  100));
		if(RandomX == 2) m_MakePathList.back()->FindPath(D3DXVECTOR3(MidX, 3, MidZ), D3DXVECTOR3(100, 3, -100));
		if(RandomX == 3) m_MakePathList.back()->FindPath(D3DXVECTOR3(MidX, 3, MidZ), D3DXVECTOR3(0,   3,    0));
	}	
}

void AI::UpdateAttack(const InputState & refInputState)
{
	for(int i = 0; m_MakePathList.size() > i; ++i)
	{	
		float MidX = 0;
		float MidZ = 0;
		float NumbersOf = 0;

		for(int k = 0; m_SoldierAttackinglist.at(i).size() > k; ++k)
		{
			D3DXVECTOR4 pos;
			D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &m_SoldierAttackinglist.at(i).at(k)->GetWorldMatrix());

			MidX += pos.x;
			MidZ += pos.z;

			++NumbersOf;	
		}

		MidX /= NumbersOf;
		MidZ /= NumbersOf;

		if(m_MakePathList.at(i)->m_foundGoal == false) m_MakePathList.at(i)->FindPath(D3DXVECTOR3(MidX, 3, MidZ), D3DXVECTOR3(-95, 3,  -95));

		if(m_MakePathList.at(i)->m_foundGoal == true)
		{
			for(int j = 0; m_SoldierAttackinglist.at(i).size() > j; ++j)
			{
				if(	m_SoldierAttackinglist.at(i).at(j)->m_GotPath == false)
				{
					m_SoldierAttackinglist.at(i).at(j)->SetPath(m_MakePathList.at(i)->GetPath());

					m_SoldierAttackinglist.at(i).at(j)->m_GotPath = true;
					m_SoldierAttackinglist.at(i).at(j)->m_GotDestination = false;					
				}
			}

			bool AllInPlace = true;

			for(int j = 0; m_SoldierAttackinglist.at(i).size() > j; ++j)
			{
				if(m_SoldierAttackinglist.at(i).at(j)->m_GotDestination != true) AllInPlace = false;
			}

			if(AllInPlace == true)
			{
				for(int j = 0; m_SoldierAttackinglist.at(i).size() > j; ++j)
				{
					m_SoldierAttackinglist.at(i).at(j)->m_GotPath = false;
				}
				m_MakePathList.at(i) = new PathFinding(m_pLevel);
			}
		}
	}

	for(int i = 0; m_SoldierAttackinglist.size() > i; ++i)
	{
		for(int j = 0; m_SoldierAttackinglist.at(i).size() > j; ++j)
		{
			D3DXVECTOR4 posSol;
			D3DXVec3Transform(&posSol, &D3DXVECTOR3(0, 0, 0), &m_SoldierAttackinglist.at(i).at(j)->GetWorldMatrix());	

			

			if(m_SoldierAttackinglist.at(i).at(j)->FoundTargetToAttack() == false)
			{
				for(int k = 0; m_BuildinglistEnemy.size() > k; ++k)
				{
					int Width = m_BuildinglistEnemy.at(k)->GetX();

					D3DXVECTOR4 pos;
					D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &m_BuildinglistEnemy.at(k)->GetWorldMatrix());	

					D3DXVECTOR4 Distance4 = posSol - pos;
					NxVec3 Distance(Distance4.x, Distance4.y, Distance4.z);
					float newLenght = Distance.magnitude() - Width;

					if(newLenght <= m_SoldierAttackinglist.at(i).at(j)->GetRange())
					{
						m_SoldierAttackinglist.at(i).at(j)->IsAttacking(m_BuildinglistEnemy.at(k));
					}
				}

				for(int k = 0; m_SoldierEnemylist.size() > k; ++k)
				{
					D3DXVECTOR4 pos;
					D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &m_SoldierEnemylist.at(k)->GetWorldMatrix());	

					D3DXVECTOR4 Distance4 = posSol - pos;
					NxVec3 Distance(Distance4.x, Distance4.y, Distance4.z);
					float newLenght = Distance.magnitude();

					if(newLenght <= m_SoldierAttackinglist.at(i).at(j)->GetRange())
					{
						m_SoldierAttackinglist.at(i).at(j)->IsAttacking(m_SoldierEnemylist.at(k));
					}
				}
			}

			else
			{
				m_SoldierAttackinglist.at(i).at(j)->Attack(refInputState.GetDeltaTime());
			}
		}
	}
}

void AI::CheckDefence(const InputState & refInputState)
{
	if( m_IsUnderAttack == false)
	{
		for(int i = 0; m_SoldierEnemylist.size() > i; ++i)
		{
			D3DXVECTOR4 pos;
			D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &m_SoldierEnemylist.at(i)->GetWorldMatrix());	

			if(pos.x > 30 && pos.z > 30)
			{
				m_IsUnderAttack = true;
				m_DefencePathList.clear();

				for(int j = 0; m_Soldierlist.size() > j; ++j)
				{
					D3DXVECTOR4 oldpos;
					D3DXVec3Transform(&oldpos, &D3DXVECTOR3(0, 0, 0), &m_Soldierlist.at(j)->GetWorldMatrix());

					PathFinding* Path = new PathFinding(m_pLevel);
					Path->FindPath(D3DXVECTOR3(oldpos.x, oldpos.y, oldpos.z) , D3DXVECTOR3(pos.x, pos.y, pos.z));

					m_DefencePathList.push_back(Path);
				}
			}
		}
	}

	else
	{
		for(int i = 0; m_Soldierlist.size() > i; ++i)
		{
			if(m_Soldierlist.at(i)->GetPath().size() != 0)
			{
				m_Soldierlist.at(i)->Move();
				
				if(m_Soldierlist.at(i)->FoundTargetToAttack() == false)
				{
					D3DXVECTOR4 posSol;
					D3DXVec3Transform(&posSol, &D3DXVECTOR3(0, 0, 0), &m_Soldierlist.at(i)->GetWorldMatrix());

					for(int j = 0; m_SoldierEnemylist.size() > j; ++j)
					{						
						D3DXVECTOR4 pos;
						D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &m_SoldierEnemylist.at(j)->GetWorldMatrix());	

						D3DXVECTOR4 Distance4 = posSol - pos;
						NxVec3 Distance(Distance4.x, Distance4.y, Distance4.z);
						float newLenght = Distance.magnitude();

						if(newLenght <= m_Soldierlist.at(i)->GetRange())
						{
							m_Soldierlist.at(i)->IsAttacking(m_SoldierEnemylist.at(j));
						}
					}
				}

				else
				{
					D3DXVECTOR4 posSol;
					D3DXVec3Transform(&posSol, &D3DXVECTOR3(0, 0, 0), &m_Soldierlist.at(i)->GetWorldMatrix());

					bool Attack = false;

					for(int j = 0; m_SoldierEnemylist.size() > j; ++j)
					{						
						D3DXVECTOR4 pos;
						D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &m_SoldierEnemylist.at(j)->GetWorldMatrix());	

						D3DXVECTOR4 Distance4 = posSol - pos;
						NxVec3 Distance(Distance4.x, Distance4.y, Distance4.z);
						float newLenght = Distance.magnitude();

						if(newLenght <= m_Soldierlist.at(i)->GetRange())
						{
							m_Soldierlist.at(i)->IsAttacking(m_SoldierEnemylist.at(j));
							Attack = true;
						}
					}

					if(Attack == true)	m_Soldierlist.at(i)->Attack(refInputState.GetDeltaTime());
					else m_Soldierlist.at(i)->m_IsAttacking = false;
					
				}

				if(m_Soldierlist.at(i)->GetPath().size() == 0)
				{
					for(int j = 0; m_SoldierEnemylist.size() > j; ++j)
					{
						D3DXVECTOR4 pos;
						D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &m_SoldierEnemylist.at(j)->GetWorldMatrix());

						D3DXVECTOR4 oldpos;
						D3DXVec3Transform(&oldpos, &D3DXVECTOR3(0, 0, 0), &m_Soldierlist.at(i)->GetWorldMatrix());	

						if(pos.x > 30 && pos.z > 30)
						{
							m_DefencePathList.at(i) = new PathFinding(m_pLevel);
							m_DefencePathList.at(i)->FindPath(D3DXVECTOR3(oldpos.x, oldpos.y, oldpos.z) , D3DXVECTOR3(pos.x, pos.y, pos.z));
						}
					}					
				}				
			}
		}

		for(int i = 0; m_DefencePathList.size() > i; ++i)
		{
			if(m_DefencePathList.at(i)->m_foundGoal == false)
			{					
				m_DefencePathList.at(i)->FindPath(D3DXVECTOR3(70, 3, 70), D3DXVECTOR3(70, 3, 70));
			}
			
			else if(m_Soldierlist.at(i)->GetPath().size() == 0)
			{
				m_Soldierlist.at(i)->SetPath(m_DefencePathList.at(i)->GetPath());
				m_Soldierlist.at(i)->m_GotPath = true;
				m_Soldierlist.at(i)->m_GotDestination = false;
			}
		}
	}

	bool IsSafe = true;

	for(int i = 0; m_SoldierEnemylist.size() > i; ++i)
	{
		D3DXVECTOR4 pos;
		D3DXVec3Transform(&pos, &D3DXVECTOR3(0, 0, 0), &m_SoldierEnemylist.at(i)->GetWorldMatrix());	

		if(pos.x > 30 && pos.z > 30)
		{
			IsSafe = false;
		}
	}

	if(IsSafe == true)
	{
		m_IsUnderAttack = false;
	}
}

bool AI::GetIfDefeated()
{
	if(m_IsMainStructure == false && m_Money < 4000)
	{
		return true;
	}

	else return false;
}